﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FireStarter.CodeGeneration;
using FireStarterModeller.BO;
using NUnit.Framework;

namespace FireStarter.Test.CodeGeneration
{
    [TestFixture]
    public class TestUITestProjectGenerator : TestProjectGeneratorBase
    {
        protected override IProjectGenerator CreateProjectGenerator()
        {
            return new UITestProjectGenerator();
        }

        protected override IProjectGenerator CreateProjectReadyToGenerate()
        {
            UITestProjectGenerator projectGenerator = new UITestProjectGenerator();
            CreateSolutionGenerator(projectGenerator);
            projectGenerator.ProjectPath = GetRootedPath(Path.Combine(TEST_PROJECTS_FOLDER, GetRandomString()));
            projectGenerator.ProjectName = "TestProject.Test.UI";
            //projectGenerator.SolutionGenerator.ClassDefsString = TestUtilsCodeGen.GetRandomString();
            projectGenerator.ModelSolution.BOProjectName = GetRandomString();
            projectGenerator.ModelSolution.LogicProjectName = GetRandomString();
            projectGenerator.ModelSolution.UIProjectName = GetRandomString();
            projectGenerator.ModelSolution.BOTestProjectName = GetRandomString();

            return projectGenerator;
        }

        private void AssertProjectGeneratedCorrectly(IProjectGenerator testProjectGenerator, ProjectFileManager boProjectFileManager, ProjectFileManager logicProjectFileManager, ProjectFileManager testBOProjectFileManager, ProjectFileManager uiProjectFileManager)
        {
            string libFolderName = testProjectGenerator.SolutionGenerator.HabaneroReferencePath;
            libFolderName = Utilities.GetRelativePath(libFolderName, testProjectGenerator.ProjectPath);
            string projectFileName = testProjectGenerator.ProjectFileName;
            Assert.IsTrue(Directory.Exists(testProjectGenerator.ProjectPath));
            Assert.IsTrue(File.Exists(projectFileName));
            ProjectFileManager projectFileManager = new ProjectFileManager(projectFileName, CodeTemplateEnum.CSharpVisualStudio2005);
            Assert.AreEqual(OutputType.Library, projectFileManager.OutputType);
            string projectFileContents = File.ReadAllText(projectFileName);
            StringAssert.Contains(@"<Compile Include=""Properties\AssemblyInfo.cs"" />", projectFileContents);
            AssertHasProjectReference(projectFileContents, testProjectGenerator, boProjectFileManager);
            AssertHasProjectReference(projectFileContents, testProjectGenerator, uiProjectFileManager);
            if (logicProjectFileManager != null)
            {
                AssertHasProjectReference(projectFileContents, testProjectGenerator, logicProjectFileManager);
            }
            if (testBOProjectFileManager != null)
            {
                AssertHasProjectReference(projectFileContents, testProjectGenerator, testBOProjectFileManager);
            }
            AssertHasReference(projectFileContents, string.Format(@"{0}\Habanero.Base.dll", libFolderName));
            AssertHasReference(projectFileContents, string.Format(@"{0}\Habanero.BO.dll", libFolderName));
            AssertHasReference(projectFileContents, string.Format(@"{0}\Habanero.DB.dll", libFolderName));
            AssertHasReference(projectFileContents, string.Format(@"{0}\nunit.framework.dll", libFolderName));
        }

        [Test]
        public void TestCreateLogicProjectGenerator()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IProjectGenerator projectGenerator = CreateProjectGenerator();
            //---------------Test Result -----------------------
            Assert.IsNotNull(projectGenerator);
            Assert.IsInstanceOf(typeof(UITestProjectGenerator), projectGenerator);
        }

        [Test]
        public void TestGenerate_Creates_ProjectFile_IfDoesNotExist()
        {
            //---------------Set up test pack-------------------
            IProjectGenerator projectGenerator = CreateProjectReadyToGenerate();
            ProjectFileManager boProjectFileManager = CreateRandomProjectFile(projectGenerator.ModelSolution.BOProjectName);
            ProjectFileManager logicProjectFileManager = CreateRandomProjectFile(projectGenerator.ModelSolution.LogicProjectName);
            ProjectFileManager uiProjectFileManager = CreateRandomProjectFile(projectGenerator.ModelSolution.UIProjectName);
            ProjectFileManager testBOProjectFileManager = CreateRandomProjectFile(projectGenerator.ModelSolution.BOTestProjectName);
            //---------------Assert Precondition----------------
            Assert.IsFalse(File.Exists(projectGenerator.ProjectFileName));
            //---------------Execute Test ----------------------
            projectGenerator.Generate();
            //---------------Test Result -----------------------
            AssertProjectGeneratedCorrectly(projectGenerator, boProjectFileManager, logicProjectFileManager, testBOProjectFileManager, uiProjectFileManager);
        }
    }
}
